package com.itheima.demo;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.SQLException;

public class DemoDataSource {
    @Test
    public void testDruid() throws Exception {
        //创建数据库连接池对象
        DruidDataSource dds = new DruidDataSource();
        //设置连接数据库四大特性
        dds.setDriverClassName("com.mysql.jdbc.Driver");
        dds.setUrl("jdbc:mysql://127.0.0.1:3306/db1");
        dds.setUsername("root");
        dds.setPassword("root");

        //获取数据库连接对象
        Connection c = dds.getConnection();
        System.out.println(c);

    }

    @Test
    public void testIocDruid() throws Exception {
        //创建Ioc容器
        ApplicationContext ac = new ClassPathXmlApplicationContext();

        //从Ioc容器中获取数据
        DruidDataSource ds = ac.getBean(DruidDataSource.class);

        //获取链接库数据的对象
        Connection c = ds.getConnection();
        System.out.println(c);
    }
}
